package org.example.partten.builder.builder.post;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.dao.dao.post.PPostDao;
import org.example.partten.builder.builder.Builder;
import org.example.partten.builder.product.Product;
import org.example.dao.entity.post.PPost;
import org.example.exception.ServerErrorException;
import org.example.partten.builder.product.post.PPostDto;
import org.example.partten.factory.ImageListFactory;
import org.example.partten.factory.IsLikeFactory;
import org.example.partten.factory.LabelsFactory;
import org.example.partten.factory.UserInfoFactory;
import org.example.skytool.commom.ContextUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 22449
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
public class PostBuilder extends Builder {

    private PPostDto product;

    @Resource
    private ContextUtil contextUtil;

    @Resource
    private ImageListFactory postImageListFactory;

    @Resource
    private IsLikeFactory postIsLikeFactory;

    @Resource
    private LabelsFactory postLabelsFactory;

    @Resource
    private UserInfoFactory postUserInfoFactory;

    @Resource
    private PPostDao pPostDao;

    public PostBuilder setPost(PPost post){
        product = new PPostDto();
        product.setPPost(post);
        return this;
    }

    public PostBuilder setPost(String postId){
        product = new PPostDto();
        product.setPPost(pPostDao.selectById(postId));
        return this;
    }

    @Override
    public Product createProduct() {
        if (product==null){
            throw new ServerErrorException("非法操作，请使用Builder构造Post");
        }
        if(product.getPPost()==null){
            throw new ServerErrorException("非法操作，请为builder传递post");
        }
        createIsLike(product.getPPost().getId().toString(),
                contextUtil.getId().toString());
        createLabels(product.getPPost().getId().toString());
        createUserInfo(product.getPPost().getUserId().toString());
        createImageList(product.getPPost().getId().toString());
        return product;
    }

    /**
     * 创建userInfo
     */
    private void createUserInfo(String userId){
        product.setUserInfo( postUserInfoFactory.createUserInfo(userId));
    }

    /**
     * 创建imageList
     */
    private void createImageList(String linkId){
        product.setImageList(postImageListFactory.createImageList(linkId));
    }

    /**
     * 创建isLike
     */
    private void createIsLike(String id1,String id2){
        product.setIsLike(postIsLikeFactory.isLike(id1,id2));
    }

    /**
     * 创建labels
     */
    private void createLabels(String linkId){
        product.setLabels(postLabelsFactory.createLabelList(linkId));
    }


}
